package com.jxpanda.spring.module.auth.core.token;

import com.jxpanda.spring.module.auth.core.authentication.token.UserDetailsAuthenticationToken;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.core.OAuth2Token;
import reactor.core.publisher.Mono;

import java.util.UUID;
import java.util.function.BiFunction;

@Builder
@RequiredArgsConstructor
public class DefaultReactiveOAuth2TokenGenerator<T extends OAuth2Token> implements ReactiveOAuth2TokenGenerator<T> {

    private final int expiresIn;

    private final BiFunction<UserDetailsAuthenticationToken<? extends UserDetails>, String, T> tokenBuilder;

    @Override
    public <U extends UserDetails> Mono<T> generate(UserDetailsAuthenticationToken<U> userDetailsAuthenticationToken) {
        String tokenValue = UUID.randomUUID().toString();
        return Mono.just(tokenBuilder.apply(userDetailsAuthenticationToken, tokenValue));
    }


}
